///////////////////////////////////////////////////////////////////////////////
//	FILENAME:	eTImageUI.m
//	SUMMARY:	Inspector for an eTImage annotation (uses eTImageComponent)
//	SUPERCLASS:	Object
//	INTERFACE:	None
//	PROTOCOLS:	<Inspectable>
//	AUTHOR:		Rohit Khare and Tom Zavisca
//	COPYRIGHT:	(c) 1994 California Institure of Technology, eText Project
///////////////////////////////////////////////////////////////////////////////
//	IMPLEMENTATION COMMENTS
//		This inspector has the expressive power to control an eTImage and its
//	eTImageComponent. The class is also written so that it can be "shared" 
//	by chaining it together with another inspecto (by inheritance or by
//	delegation).
///////////////////////////////////////////////////////////////////////////////
//	HISTORY
//	11/26/94:	Added pull-down menu, image-from-caption options.
//	07/19/94:	Rewritten from scratch (RK) from remains of ImageAnnotationUI.
///////////////////////////////////////////////////////////////////////////////

#import "eTImageUI.h"

@implementation eTImageUI
//	id		etImage
//	id		controlPanel
//	id		controlView
//	id		componentNameField
//	id		descriptionField
//	id		linkSwitch
//	id		heightField
//	id		widthField
+ new
{
	static eTImageUI *ui = nil;
	
	if (!ui) {
		ui = [[eTImageUI alloc] init];
	}
	return ui;
}
- init
{
	char        buf[MAXPATHLEN];
	NXBundle   *bundle;

	[super init];
	bundle = [NXBundle bundleForClass:[eTImageUI class]];
	if ( [bundle getPath:buf forResource:"eTImageUI" ofType:"nib"] ) {
		[NXApp loadNibFile:buf owner:self withNames:NO];
	} else {
		NXLogError("NIB not found: eTImageUI");
	}
	controlView = [controlPanel contentView];
	[descriptionField setDelegate:self];
	[descriptionField setCharFilter:(NXCharFilterFunc)NXFieldFilter];
	return self;
}
- free {return self;}

- setAnnotation:neweTImage
{
	NXSize *size;
	
	if (etImage == neweTImage) return self;
	if (![neweTImage isKindOf:[eTImage class]]) return self;
	
	// do we have to force an unloading of the data currently in the panel?
	// if (etImage) [self editDescription:descriptionField];
	etImage = neweTImage;
	size = [etImage size];
	[heightField setFloatValue:size->height];
	[widthField setFloatValue:size->width];
	[descriptionField setText:[etImage description]];
	[componentNameField setStringValue:
		[[etImage imageComponent] componentName]];
	[linkSwitch setState:[[etImage imageComponent] isLinked]];
	return self;
}

- chooseImage:sender
{
	// needs to reset shouldEdit and component name
	const char *directory;
	const char *const *files;
	NXAtom	   *importTypes;
	int			i,choice;
	char 		path[MAXPATHLEN];
	id			openpanel;
	id			newImageComponent;
	NXSize		*size;

	openpanel = (id)[[OpenPanel new] allowMultipleFiles:YES];
	importTypes = (NXAtom *)[NXImage imageFileTypes];
	[openpanel setTreatsFilePackagesAsDirectories:YES];
	[openpanel chooseDirectories:NO];
	i = [openpanel runModalForTypes:importTypes];
	if (!i) return self;
	files = [openpanel filenames];
	directory = [openpanel directory];
	strcpy(path, directory);
	strcat(path, "/");
	strcat(path, files[0]);
	choice = NXRunAlertPanel("eTImage","Do you want to make a link to %s or copy it?", "Copy", "Link", NULL,path);
	
	newImageComponent = [[eTImageComponent alloc]
							initInDoc:[etImage etDoc] 
							linked: (choice == NX_ALERTALTERNATE)];
	[newImageComponent readComponentFromPath:NXUniqueString(path)];
	[etImage setImageComponent:newImageComponent];
	
	//NEW: Added by RK 12/23
	[etImage setAltImageComponent:nil];
	
	[[etImage etDoc] touch];
	[[etImage imageComponent] touch];
	[etImage updateGraphics];
//	this duplicates code from init:above
	size = [etImage size];
	[heightField setFloatValue:size->height];
	[widthField setFloatValue:size->width];
	[descriptionField setText:[etImage description]];
	[componentNameField setStringValue:
		[[etImage imageComponent] componentName]];
	[linkSwitch setState:[[etImage imageComponent] isLinked]];
	return self;
}

- pasteImage:sender
{
	if ([NXImage canInitFromPasteboard:[Pasteboard newName:NXGeneralPboard]]) {
		NXSize		*size;
		id newImageComponent = [[eTImageComponent alloc]
								initInDoc:[etImage etDoc] linked: NO];
		[newImageComponent readComponentFromPboard:
								[Pasteboard newName:NXGeneralPboard]];
		[etImage setImageComponent:newImageComponent];

		//NEW: Added by RK 12/23
		[etImage setAltImageComponent:nil];

		[[etImage etDoc] touch];
		[[etImage imageComponent] touch];
		[etImage updateGraphics];
	//	this duplicates code from init:above
		size = [etImage size];
		[heightField setFloatValue:size->height];
		[widthField setFloatValue:size->width];
		[descriptionField setText:[etImage description]];
		[componentNameField setStringValue:
			[[etImage imageComponent] componentName]];
		[linkSwitch setState:[[etImage imageComponent] isLinked]];
	} else {
		NXBeep();
	}
	return self;
}

- captionImage:sender {
	[etImage setCaptionMode];

	//NEW: Added by RK 12/23
	//[etImage setAltImageComponent:nil];
	// Taken out 2/14/95
	
	[etImage updateGraphics];

	return self;
}

- uncaptionImage:sender {
	[etImage setImageComponent:[etImage imageComponent]];
	[etImage updateGraphics];
	return self;
}

- editDescription: sender	// descriptionField is a Text.
{
	char		*buf;
	int			length;

	length = [descriptionField textLength]+1;
	buf = malloc(length * sizeof(char));
	[descriptionField getSubstring:(buf) start:0 length:length];
	(buf)[length]=0;
	[etImage setDescription:buf];
	free(buf);
	return self;
}
- editComponentName: sender
{
	[[etImage imageComponent] setComponentName:
		NXUniqueString([componentNameField stringValue])];
	[componentNameField setStringValue:
		[[etImage imageComponent] componentName]];
	[[etImage etDoc] touch];
	[[etImage imageComponent] touch];
	return self;
}
- editSize:sender
{
	NXSize size;

	size.height = fabs([heightField floatValue]);
	size.width = fabs([widthField floatValue]);
	[etImage setSize:&size];
	// are size-changes are only a "run-time" feature?
	[etImage updateGraphics];
	return self;
}

- (const NXAtom *) types
	{static NXAtom types[2]={NULL,NULL}; 
	 if(!types[0]) types[0]=NXUniqueString("Image Properties"); return types;} 
- inspectorForType:(const char *) type
	{if(strcmp(type, NXUniqueString("Image Properties")))
		NXLogError("Massive Inspector Failure: Asked eTImageUI for: %s", type);
	 return controlView;}
- resignInspector: (View *) oldInspector ofType: (const char *) type
{
	// have to do dirty work here 
	
	return self;
}
- activateInspector:(View *) newInspector ofType:(const char *) type
{
	[[NXApp inspector] makeFirstResponder:descriptionField]; 

	//cleanup work here!

	 return self;
}
- (const char *) inspectorTitle
	{return NXUniqueString("Image");}


- textDidEnd:sender endChar:(unsigned short)whyEnd
{	// should this be checked programatically on setAnnotation:?
	if(descriptionField==sender){ 
		[self editDescription:sender];
	}
	return self;
}
@end